<?php
/**
 * 段位
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/1
 * Time: 14:27
 */

namespace app\admin\controller;


use think\Db;

class Dangrading extends Base
{
    /**
     * 段位列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('exp_level')
                ->join('up_coupon', 'coupon_id = el_price_coupon', 'left')
                ->count();// 获取总条数
            $lists = Db::name('exp_level')
                ->join('up_coupon', 'coupon_id = el_price_coupon', 'left')
                ->field('el_id, el_name, el_min, el_max, el_price_score, coupon_name, el_level')
                ->page($Nowpage,$limits)
                ->order('el_level desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 段位添加
     * @return mixed|\think\response\Json
     */
    public function dangradingAdd(){
        if(request()->isPost()){
            $params = input('param.');
            if($params['el_min'] > $params['el_max']){
                return json(['code' => 100, 'msg' => '经验值下限不可高于上限']);
            }
            $data = [
                'el_name' => $params['el_name'],
                'el_min' => $params['el_min'],
                'el_max' => $params['el_max'],
                'el_level' => $params['el_level'],
                'el_price_score' => $params['el_price_score'],
                'el_price_coupon' => $params['coupon_id']
            ];
            $res = Db::table('up_exp_level')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $couponList = Db::table('up_coupon')
            ->field('coupon_id, coupon_name')
            ->where('coupon_status', 1)
            ->where('coupon_online', 1)
            ->where('coupon_type', 3)
            ->select();
        $this->assign('couponList', $couponList);
        return $this->fetch('dangrading/dangradingadd');
    }
    
    /**
     * 段位编辑
     * @return mixed|\think\response\Json
     */
    public function dangradingEdit(){
        if(request()->isPost()){
            $params = input('param.');
            if($params['el_min'] > $params['el_max']){
                return json(['code' => 100, 'msg' => '经验值下限不可高于上限']);
            }
            $data = [
                'el_name' => $params['el_name'],
                'el_min' => $params['el_min'],
                'el_max' => $params['el_max'],
                'el_level' => $params['el_level'],
                'el_price_score' => $params['el_price_score'],
                'el_price_coupon' => $params['coupon_id']
            ];
            $res = Db::table('up_exp_level')->where('el_id', $params['el_id'])->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $couponList = Db::table('up_coupon')
            ->field('coupon_id, coupon_name')
            ->where('coupon_status', 1)
            ->where('coupon_online', 1)
            ->where('coupon_type', 3)
            ->select();
        $id = input('id');
        $elInfo = Db::table('up_exp_level')->where('el_id', $id)->find();
        $this->assign([
            'couponList' => $couponList,
            'elInfo' => $elInfo
        ]);
        return $this->fetch('dangrading/dangradingedit');
    }
    
    /**
     * 段位删除
     * @return \think\response\Json
     */
    public function dangradingDel(){
        $el_id = input('id');
        $res = Db::table('up_exp_level')->delete($el_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}